package com.jason.www.widget.itemdecoration;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.View;

import com.jason.www.R;
import com.jason.www.base.RunApplication;

import androidx.annotation.ColorInt;
import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

/**
 * @author：Jason
 * @date：2020/7/29 09:58
 * @email：1129847330@qq.com
 * @description: 自定义recyclerview的分割线，可定义颜色和粗细
 */
public class CommonItemDecoration extends RecyclerView.ItemDecoration {
    private boolean mDrawFirst = true;//是否绘制第一个
    private boolean mDrawLast = false;//是否绘制最后一个
    private int mDividerWidth = 1;//分割线粗细
    private int mSpace = 0;//左右间距
    private Rect mRect = new Rect(0, 0, 0, 0);
    private Paint mPaint = new Paint();
    private int mOrientation;

    /**
     * @param orientation  recyclerview的滑动方向
     * @param color        分割线颜色
     * @param dividerWidth 分割线粗细
     * @param space        左右间距或者上下间距
     * @param drawFirst    是否预留头部margin，只会预留间隔，不会绘制分割线
     * @param drawLast     是否预留尾部margin，只会预留间隔，不会绘制分割线
     */
    public CommonItemDecoration(int orientation, @ColorInt int color, int dividerWidth, int space, boolean drawFirst, boolean drawLast) {
        mOrientation = orientation;
        if (dividerWidth > 0) {
            mDividerWidth = dividerWidth;
        }
        mSpace = space;
        mDrawFirst = drawFirst;
        mDrawLast = drawLast;
        mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
        mPaint.setColor(color);
    }

    /**
     * 在item的四周留下边距，效果和margin类似，留下边距后就可以在onDraw()方法里绘制分割线了
     *
     * @param outRect
     * @param view
     * @param parent
     * @param state
     */
    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        super.getItemOffsets(outRect, view, parent, state);
        int position = parent.getChildAdapterPosition(view);
        if (mOrientation == LinearLayoutManager.VERTICAL) {
            if (position == 0) {
                if (!mDrawFirst) {
                    //第一个itemView的顶部不预留分割线的位子，故而top为0
                    outRect.set(mSpace, 0, mSpace, mDividerWidth);
                } else {
                    outRect.set(mSpace, mDividerWidth, mSpace, mDividerWidth);
                }
            } else if (position == parent.getAdapter().getItemCount() - 1) {
                if (!mDrawLast) {
                    //最后一个item不预留分割线的位子
                    outRect.set(mSpace, 0, mSpace, 0);
                } else {
                    outRect.set(mSpace, 0, mSpace, mDividerWidth);
                }
            } else {
                outRect.set(mSpace, 0, mSpace, mDividerWidth);
            }
        } else {
            if (position == 0) {
                if (!mDrawFirst) {
                    //第一个itemView的顶部不预留分割线的位子，故而left为0
                    outRect.set(0, mSpace, mDividerWidth, mSpace);
                } else {
                    outRect.set(mDividerWidth, mSpace, mDividerWidth, mSpace);
                }
            } else if (position == parent.getAdapter().getItemCount() - 1) {
                if (!mDrawLast) {
                    outRect.set(0, mSpace, 0, mSpace);
                } else {
                    outRect.set(0, mSpace, mDividerWidth, mSpace);
                }
            } else {
                outRect.set(0, mSpace, mDividerWidth, mSpace);
            }
        }
    }

    /**
     * 在 item 内容之下绘制图形
     *
     * @param c
     * @param parent
     * @param state
     */
    @Override
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
        super.onDraw(c, parent, state);
        if (mOrientation == LinearLayoutManager.VERTICAL) {
            drawVertical(c, parent);
        } else {
            drawHorizontal(c, parent);
        }
    }

    /**
     * 在 item 内容之上绘制图形
     *
     * @param c
     * @param parent
     * @param state
     */
    @Override
    public void onDrawOver(@NonNull Canvas c, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
        super.onDrawOver(c, parent, state);
    }

    /**
     * 绘制垂直方向上的水平分割线
     *
     * @param c
     * @param parent
     */
    public void drawVertical(Canvas c, RecyclerView parent) {
        final int left = parent.getPaddingLeft() + mSpace;
        final int right = parent.getWidth() - parent.getPaddingRight() - mSpace;

        final int childCount = parent.getChildCount();
        for (int i = 0; i < childCount; i++) {
            final View child = parent.getChildAt(i);
            final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
            final int top = child.getBottom() + params.bottomMargin;
            final int bottom = top + mDividerWidth;
            mRect.set(left, top, right, bottom);
            c.drawRect(mRect, mPaint);
        }
    }

    /**
     * 绘制水平方向上的垂直分割线
     *
     * @param c
     * @param parent
     */
    public void drawHorizontal(Canvas c, RecyclerView parent) {
        final int top = parent.getPaddingTop() + mSpace;
        final int bottom = parent.getHeight() - parent.getPaddingBottom() - mSpace;

        final int childCount = parent.getChildCount();
        for (int i = 0; i < childCount; i++) {
            final View child = parent.getChildAt(i);
            final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
            final int left = child.getRight() + params.rightMargin;
            final int right = left + mDividerWidth;
            mRect.set(left, top, right, bottom);
            c.drawRect(mRect, mPaint);
        }
    }

    /**
     * 垂直方向上的水平分割线，包括头部，不包括尾部
     *
     * @param color  分割线颜色
     * @param height 分割线粗细
     * @return
     */
    public static CommonItemDecoration createVertical(@ColorInt int color, int height) {
        return new CommonItemDecoration(LinearLayoutManager.VERTICAL, color, height, 0, true, false);
    }

    /**
     * 垂直方向上的水平分割线，包含头部和尾部
     *
     * @return
     */
    public static CommonItemDecoration createVerticalWithFirstLast() {
        return new CommonItemDecoration(LinearLayoutManager.VERTICAL, ContextCompat.getColor(RunApplication.getContext(), R.color.windows_background), 1, 0, true, true);
    }

    /**
     * 垂直方向上的水平分割线，包括头部，不包括尾部
     *
     * @return
     */
    public static CommonItemDecoration createVertical() {
        return createVertical(ContextCompat.getColor(RunApplication.getContext(), R.color.windows_background), 1);
    }

    /**
     * 水平方向上的垂直分割线，默认粗细10dp，不包括头部和尾部
     *
     * @return
     */
    public static CommonItemDecoration createHorizontalWithSpace() {
        return createHorizontalWithSpace(Color.TRANSPARENT);
    }

    /**
     * 水平方向上的垂直分割线，默认粗细10dp，不包括头部和尾部
     *
     * @param color 分割线颜色
     * @return
     */
    public static CommonItemDecoration createHorizontalWithSpace(int color) {
        return createHorizontalWithSpace(color, (int) RunApplication.getContext().getResources().getDimension(R.dimen.dp_10));
    }

    /**
     * 水平方向上的垂直分割线，不包括头部和尾部，默认透明
     *
     * @param width 分割线粗细
     * @return
     */
    public static CommonItemDecoration createHorizontalWithSpace(float width) {
        return createHorizontalWithSpace(Color.TRANSPARENT, (int) width);
    }

    /**
     * 水平方向上的垂直分割线，默认粗细10dp，不包括头部和尾部
     *
     * @param color
     * @param width
     * @return
     */
    public static CommonItemDecoration createHorizontalWithSpace(int color, int width) {
        return new CommonItemDecoration(LinearLayoutManager.HORIZONTAL, color, width, 0, false, false);
    }
}